<template>
  <div class="addbox">
    <div class="boxHeader">
      <p>{{title}}</p>
      <i class="el-icon-close" @click="closeAddBox"></i>
    </div>
    <div class="addMainBox">
      <el-form
        :model="statisticalItemsDTO"
        :rules="rules"
        ref="statisticalItemsDTO"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="name" label="类目名称 :" class="name">
          <el-input v-model="statisticalItemsDTO.name"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <footer>
      <div class="cancel" @click="closeAddBox">取消</div>
      <div class="determainAdd" @click="determainAdd">确定</div>
    </footer>
  </div>
</template>

<script>
import { addItem, updateItem } from '@/api/documentModule'
export default {
  data () {
    return {
      statisticalItemsDTO: {
        item: 5,
        name: ''
      },
      id: 5,
      index: 1,
      rules: {
        name: [
          { required: true, message: '请输入类目名称' },
          {
            min: 1,
            max: 10,
            message: '长度需小于10个字符',
            trigger: ['blur', 'change']
          }
        ]
      },
      title: ''
    }
  },
  props: {
    ifEditOrAdd: Number,
    updateItemInfo: Object
  },
  methods: {
    closeAddBox () {
      this.$store.state.ifShowCover = false
      this.$store.state.showEditBox = false
    },
    async determainAdd () {
      if (this.ifEditOrAdd === 1) {
        const res = await addItem(this.statisticalItemsDTO)
        if (res.data.code === 200) {
          this.$message({
            message: res.data.message,
            type: 'success',
            duration: 1000
          })
        }
      } else if (this.ifEditOrAdd === 2) {
        const res = await updateItem(this.updateItemInfo.id, this.statisticalItemsDTO.name)
        if (res.data.code === 200) {
          this.$message({
            message: res.data.message,
            type: 'success',
            duration: 1000
          })
        }
      }
      this.$store.state.ifShowCover = false
      this.$store.state.showEditBox = false
      this.$store.dispatch('file/updateCateDataList', {
        index: this.index,
        item: this.id,
        pageSize: 10
      })
    }
  },
  created () {
    // console.log(this.ifEditOrAdd)
    if (this.ifEditOrAdd === 1) {
      this.title = '添加类目'
    } else if (this.ifEditOrAdd === 2) {
      this.title = '编辑类目'
      this.statisticalItemsDTO.name = this.updateItemInfo.name
    }
  }
}
</script>

<style scoped lang="scss">
.addbox {
  top: 0px !important;
  .addMainBox {
    position: relative;
    height: 534px;
    border-bottom: solid 1px #e6e6e6;
    .name {
      position: absolute;
      top: 42px;
      left: 55px;
      ::v-deep .el-form-item__label {
        width: 168px !important;
        position: absolute;
        text-align: right;
        font-size: 16px;
        color: #333333;
      }
      ::v-deep .el-form-item__label:before {
        content: "*";
        color: #4868ff;
      }
      .el-input {
        position: absolute;
        left: 175px;
        ::v-deep .el-input__inner {
          width: 400px;
          height: 48px;
          border-radius: 6px;
          border: 1px solid rgba(220, 220, 220, 1);
          font-size: 16px;
        }
      }
      ::v-deep .el-form-item__error {
        position: absolute;
        left: 175px;
        top: 50px;
        width: 400px;
      }
    }
    .Correlation {
      .select {
        position: absolute;
        width: 168px;
        height: 22px;
        font-size: 16px;
        font-weight: 400;
        line-height: 22px;
        text-align: right;
        top: 53px;
        left: 42px;
      }
      .select::before {
        content: "*";
        color: #4868ff;
      }
      .el-select {
        width: 400px;
        height: 48px;
        position: absolute;
        top: 45px;
        left: 50%;
        transform: translateX(-50%);
      }
    }
  }
}
@import "@/styles/clickBox.scss";
</style>
